package study.nzy.com.studydemo;

import android.os.Bundle;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpActivity extends Activity {

    TextView tv_text;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 1){
                tv_text.setText((String)msg.obj);
            }else if(msg.what == 2){
                tv_text.setText("POST:"+(String)msg.obj);
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http);
        tv_text = findViewById(R.id.tv_text);
    }

    public void getData(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                getHttp();
            }
        }).start();
    }

    BufferedReader bufferedReader;
    HttpURLConnection connection;

    public void getHttp() {
        try {
            URL url = new URL("https://apis.juhe.cn/fapigw/naming/query?key=ff91422e21df2b9d72861d3d3f58c58c&surname=李");
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            int code = connection.getResponseCode();
            if (code == HttpURLConnection.HTTP_OK){
                bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                StringBuffer response = new StringBuffer();
                String line;
                while ((line = bufferedReader.readLine()) != null){
                    response.append(line);
                }
                System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~");
                System.out.println(response.toString());
                Message message = new Message();
                message.obj = response.toString();
                message.what = 1;
                handler.sendMessage(message);
                System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try{
                if(bufferedReader!=null){
                    bufferedReader.close();
                }
                if (connection != null){
                    connection.disconnect();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public void postData(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                postHttp();
            }
        }).start();
    }

    public void postHttp() {
        try {
            URL url = new URL("https://apis.juhe.cn/fapigw/naming/query");
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            connection.setDoOutput(true);//开启数据输出
            //获得输出流对象用来写字节流
            //key=ff91422e21df2b9d72861d3d3f58c58c&surname=李
            OutputStream outputStream = connection.getOutputStream();
            String body ="key=ff91422e21df2b9d72861d3d3f58c58c&surname=李";
            outputStream.write(body.getBytes());
            outputStream.flush();//无论缓存是否满，触发输出输出流中缓存的数据
            outputStream.close();//关闭输出流
            int code = connection.getResponseCode();
            if (code == HttpURLConnection.HTTP_OK){
                bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                StringBuffer response = new StringBuffer();
                String line;
                while ((line = bufferedReader.readLine()) != null){
                    response.append(line);
                }
                System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~");
                System.out.println(response.toString());
                Message message = new Message();
                message.obj = response.toString();
                message.what = 2;
                handler.sendMessage(message);
                System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try{
                if(bufferedReader!=null){
                    bufferedReader.close();
                }
                if (connection != null){
                    connection.disconnect();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
